<?php
    /*
    * @copyright        [YouNet_COPYRIGHT]
    * @author           YouNet Development
    * @package          Module_Contactimporter
    * @version          2.06
    *
    */
    defined('PHPFOX') or exit('NO DICE!');
?>

<?php
if (!class_exists('Facebook'))
{
    require_once(PHPFOX_DIR.'module'.PHPFOX_DS.'contactimporter'.PHPFOX_DS.'include'.PHPFOX_DS.'component'.PHPFOX_DS.'controller'.PHPFOX_DS.'Apiconnection'.PHPFOX_DS.'facebook.php');  
}
require_once (PHPFOX_DIR . 'module' . PHPFOX_DS . 'contactimporter' . PHPFOX_DS . 'include' . PHPFOX_DS . 'service' . PHPFOX_DS . 'abstract.class.php');
class Contactimporter_Service_Facebook extends Contactimporter_Service_Abstract 
{
	protected $_oFacebook;
    
    public function __construct()    
    {       
        $this->init();        	
    }   
    
    public function init()    
    {       
        $aSetting = Phpfox::getService('contactimporter.setting')->getApiSetting('facebook');  
        $aSetting['api_params'] = unserialize($aSetting['api_params']);            
        $this->_oFacebook = $oFacebook = new FacebookSBYN(array(
            'appId'  => $aSetting['api_params']['appid'],
            'secret' => $aSetting['api_params']['secret'],
            'cookie' => true,
        ));		
    }
    
    public function getSession()
	{
		if (!$this->_oFacebook)
        {
            $this->init();  
        }
        return $this->_oFacebook->getSession();
	}

	public function setSession($session = null)
	{
		if (!$this->_oFacebook)
        {
            $this->init();  
        }        
		return $this->_oFacebook->getSession($session);				
	}
    
    public function getProfile()
	{        
        $oFacebook = $this->_oFacebook;
        if ($oFacebook)
		{			
			$uid = $oFacebook->getUser();
			try
            {
				$me = $oFacebook->api('/me');
                $aFriendCount = $oFacebook->api(array(
                    'method' => 'fql.query',
                    'query' => "SELECT friend_count FROM user WHERE uid = me()"
                ));
                $me['friend_count'] = isset($aFriendCount[0]['friend_count']) ? $aFriendCount[0]['friend_count'] : 0;
            }
            catch(YNSFacebookApiException $e)
            {
				//Phpfox_Error::set($e);
				return null;
			}
			return $me;
		}
		return null;
	}
    
    public function getFriends($iPage = 1, $iLimit = 50)
	{		       
        $oFacebook = $this->_oFacebook;
        $iOffset = ($iPage - 1) *  $iLimit;
        if ($oFacebook)
		{
			try
			{
				if (!isset($_SESSION['contactimporter']['facebook']))
                {
                    $session = $oFacebook->getSession();
                    //$friends = $oFacebook->api('/me/friends?token=' . $session['access_token'] . '&limit=' . $iLimit . '&offset=' . $iOffset);
                    $friends = $oFacebook->api('/me/friends?token=' . $session['access_token']);
                    $imgLink = "http://graph.facebook.com/%s/picture";
                    $nextpage = $friends['paging']['next'];
                    $friends = $friends['data'];
                    foreach ($friends as $i => $value)
                    {
                        $friends[$i]['pic'] = sprintf($imgLink, $value['id']);
                    }
                    $_SESSION['contactimporter']['facebook'] = $friends;
                }
                else
                {
                    $friends = $_SESSION['contactimporter']['facebook'];
                }
                $friends = array_slice($friends, $iOffset, $iLimit);
                $friends = $this->processSocialRows($friends);                
                return $friends;
			}
			catch(FacebookApiException $e)
			{
				Phpfox_Error::set($e);
			}			
		}
		return array();
	}
    
    public function sendInvitation($iId, $sMessage, $sLink)
	{        
        if (!$this->_oFacebook)
        {
            $this->init();
        }
        $oFacebook = $this->_oFacebook;
        if ($oFacebook)
		{
			if ($iId)
			{
				$me = $oFacebook->api('/me');
                $sInviterName = $me['name'];
                $sMessage = Phpfox::getPhrase('contactimporter.invitation_message_for_social', array('message' => $sMessage, 'full_name' => $sInviterName, 'host' => phpfox::getParam('core.path'), 'link' => $sLink));				
                $aParam = array (
					'message' => $sMessage,					
					'link' => Phpfox::getParam('core.path'),
                    'picture'=>'http://www.housenet.com.au/images/facebook-64x64.png',
                    'name' => Phpfox::getParam('core.global_site_title'),               
                    'caption' => Phpfox::getParam('core.path'),
                    'description' => Phpfox::getParam('core.description')
				);
                try
                {
                    $iReturn = $oFacebook->api('/'.$iId.'/feed', 'POST', $aParam);
                    return $iReturn;
                }
                catch(Exception $e)
                {                    
                    Phpfox_Error::set($e);
                    return false;
                }				
			}
		}
	}    
}
?>